package net.xju_springJdbc17_18.dao;

import net.xju_springJdbc17_18.entity.Stu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

@Repository
@Transactional
public class StuDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    public void insertStu(Stu stu) {
        String sql = "insert into newtable(name,sn) values(?,?)";
        jdbcTemplate.update(sql, stu.getName(), stu.getSn());
        int a = 10 / 0;
    }

    public List<Stu> selectAll() {
        String sql = "select * from newtable";
        BeanPropertyRowMapper<Stu> rowMapper = new BeanPropertyRowMapper<>(Stu.class);
        List<Stu> stuList = jdbcTemplate.query(sql, rowMapper);
        return stuList;
    }
    @Transactional(timeout = 2,rollbackFor = Exception.class)
    public void updateStu(Stu stu) throws FileNotFoundException {
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        String sql = "update newtable set name=?,sn=? where id=?";
        jdbcTemplate.update(sql, stu.getName(), stu.getSn(), stu.getId());
//        int a = 10 / 0;
        new FileInputStream("xxx");

    }
}
